import os
import re
import json
import copy
import numpy as np
import sys 
sys.path.append("../..")
from package.utility import find_targets
from package.utility import ensure_exists
import pandas as pd

def InputdirPath(topdir): #输入一个算例文件夹名，输出一个存着文件夹地址的list
    "遍历文件夹得到文件夹地址"
    topdir = os.path.abspath(topdir)
    ensure_exists(topdir)

    print(f">> scan runcase under {topdir}") 
    runcases = find_targets(topdir, "sub.json", suffix="-xtype f") 
    runcase_num = len(runcases)
    print(f">> {runcase_num} runcases found!")
    return runcases

def mycsv(savedir,basename,dict):
    df = pd.DataFrame(dict)
    df = pd.DataFrame(dict)
    df = df.sort_index(axis=1)
    df = df.sort_values(by='numprocess', ascending=True)
    df.set_index(["numprocess"], inplace=True)
    
    # print(df)
    # exit(0)
    writer = pd.ExcelWriter(os.path.join(savedir, basename+".xlsx"))
    df.to_excel(writer)
    writer.save()

def main(mydir,savepath):
    result_list = []
    Inpttdirlist = InputdirPath(mydir)

    for i in Inpttdirlist:
        data_dict = {}
        filepath = os.path.dirname(i)
        if(os.path.exists(f"{filepath}/pbsinfo.json")):
            infofile = "pbsinfo.json"
        elif(os.path.exists(f"{filepath}/slurminfo.json")):
            infofile = "slurminfo.json"
        with open(os.path.join(filepath,infofile), 'r') as fo:
            jsonData = fo.read()
            text = json.loads(jsonData)
            casename = text["casename"].split(".")[0]
            numprocess = text["argument"]["numprocess"]
            data_dict["casename"] = casename
            data_dict["numprocess"] = numprocess
        
        with open(f"{i}", 'r') as fo:
            jsonData = fo.read()
            text = json.loads(jsonData)
            for i in text:
                data_dict[f"{i}+Setup"] = []
                programdata = text[i]
                for j in programdata:
                    if j.split("_")[-1] == "Setup":
                        avgtime = float(programdata[j]["tave"])
                        data_dict[f"{i}+Setup"].append(avgtime)
        result_list.append(data_dict)
    
    for tmpdict in result_list:
        for j in tmpdict:
            if j.split("+")[-1] == "Setup":
                tmpdict[j] = np.mean(tmpdict[j])

    mycsv(savepath,"230613_alltest",result_list)

if __name__=="__main__":
    mydir = "/home/mrz/data/230612-alltest/D0612T1727"
    savepath = "/home/mrz/data/230612-alltest/D0612T1727"
    main(mydir,savepath)